package com.fruit.util;

import java.security.MessageDigest;

/**
 * 提供各种常规加密，如MD2，MD5，SHA-1，SHA-256，SHA-384，SHA-512
 * @author Administrator
 *
 */
public class SecurityUtil
{
	static char[] hexChars={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	
	public static String messageDigest(String msg,Algorithm algorithm)
	{
		try{
			MessageDigest md=MessageDigest.getInstance(algorithm.getKey());
			md.update(msg.getBytes());
			return toHexString(md.digest());
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	private static String toHexString(byte[] bb)
	{
		StringBuilder sb=new StringBuilder();
		for(byte b:bb)
		{
			sb.append(hexChars[(b>>4)&0x0f]);
			sb.append(hexChars[b&0x0f]);
		}
		return sb.toString();
	}
	public static void main(String[] args)
	{
		System.out.println(messageDigest("123456", Algorithm.SHA_256));
	}
}
